package com.objectdraw.client.objectdraw;
import java.awt.*;

/**
 * Instantiated to store a text object. 
 * 
 * @author jgulik, cfruehan
 *
 */
public class TextObject implements IDrawingObject{
	Point startingPosition;
	Color myColor;
	String mytext;
	Font font;
	
	/**
	 * constructor used to create a text object without a string prepared
	 * @param p point to begin drawing
	 * @param c color of text
	 * @param f font of text
	 */
	public TextObject(Point p, Color c, Font f){
		startingPosition=p;
		myColor=c;
		this.font=f;
		mytext="";
	}

	/**
	 * overloaded constructor using only strings.  used for loading a prepared string in to the
	 * text object
	 * @param x1 x coord of staring point
	 * @param y1 y coord of starting point
	 * @param color color of text
	 * @param string string to be saved/printed
	 */
	public TextObject(String x1, String y1, String color, String string){
		
		if(color.equalsIgnoreCase("red")){
			myColor = Color.RED;
		}else if(color.equalsIgnoreCase("blue")){
			myColor = Color.BLUE;
		}else if(color.equalsIgnoreCase("green")){
			myColor = Color.GREEN;
		}else{
			myColor = Color.BLACK;
		}
		
		
		startingPosition= new Point(Integer.parseInt(x1), Integer.parseInt(y1));
		
		mytext = string;
		
		font = new Font("Serif", Font.BOLD, 24);
	
}
	
	/**
	 * sets text in object to s
	 * @param s sets text to this
	 */
	public void setText(String s){
		mytext=s;
	}
	
    /**
     * draws this object to the canvas
     */
	public void drawMe(Graphics g) {
		g.setFont(font);
		//Color tempColor = g.getColor();
		g.setColor(myColor);
		g.drawString(mytext, startingPosition.x,startingPosition.y); 
		//g.setColor(tempColor);
	}

	/**
	 * checks if point is in the bounding box of the object
	 */
	public boolean isPointInObject(Point p) {
		return p.x<=getLeft()+getWidth() && p.x>=getLeft() && p.y>=getTop() && p.y<=getTop()+getHeight();
	}

	/**
	 * returns height of the string 
	 */
	@SuppressWarnings("deprecation")
	public int getHeight() {
		return Toolkit.getDefaultToolkit().getFontMetrics(font).getHeight();
	}

	/**
	 * return the left side of the bounding box
	 */
	public int getLeft() {
		// TODO Auto-generated method stub
		return startingPosition.x;
	}

	/**
	 * returns the top of the bounding box
	 */
	public int getTop() {
		// TODO Auto-generated method stub
		return startingPosition.y-getHeight()*2/3;
	}

	/**
	 * returns width of string
	 */
	@SuppressWarnings("deprecation")
	public int getWidth() {
		// TODO Auto-generated method stub
		return Toolkit.getDefaultToolkit().getFontMetrics(font).stringWidth(mytext);
	}
	
	/**
	 * set color of the object to c
	 */
	public void setmycolor(Color c){
		myColor=c;
	}
	
	/**
	 * move the anchor point by (+x, +y)
	 */
	public void moveAnchor(int x, int y) {
		startingPosition.x+=x;
		startingPosition.y+=y;
	}
}
